package com.shiguiwu.springmybatis.designpattern.composite.scene.engine;

import com.shiguiwu.springmybatis.designpattern.composite.scene.aggregates.TreeRich;
import com.shiguiwu.springmybatis.designpattern.composite.scene.logic.LogicFilter;
import com.shiguiwu.springmybatis.designpattern.composite.scene.vo.EngineResult;
import com.shiguiwu.springmybatis.designpattern.composite.scene.vo.TreeNode;
import com.shiguiwu.springmybatis.designpattern.composite.scene.vo.TreeRoot;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @description: 基础引擎
 * @author: stone
 * @date: Created by 2021/8/26 20:25
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.designpattern.composite.scene.engine
 */
@Slf4j
public abstract class EngineBase extends EngineConfig implements IEngine {

    @Override
    public abstract EngineResult process(Long treeId, String userId, TreeRich treeRich, Map<String, String> decisionMatter);


    protected TreeNode engineDecisionMaker(TreeRich treeRich, Long treeId, String userId, Map<String, String> decisionMatter) {
        TreeRoot treeRoot = treeRich.getTreeRoot();
        Map<Long, TreeNode> treeNodeMap = treeRich.getTreeNodeMap();

        // 规则树根ID
        Long treeRootNodeId = treeRoot.getTreeRootNodeId();

        TreeNode treeNode = treeNodeMap.get(treeRootNodeId);
        //节点类型[NodeType]；1子叶、2果实
        while (treeNode.getNodeType().equals(1)) {
            //拿到key
            String ruleKey = treeNode.getRuleKey();
            LogicFilter logicFilter = logicFilterMap.get(ruleKey);
            //获取决策值
            String matterValue = logicFilter.matterValue(treeId, userId, decisionMatter);
            //根据值获取下一个节点信息
            Long nextNode = logicFilter.filter(matterValue, treeNode.getTreeNodeLinkList());
            treeNode = treeNodeMap.get(nextNode);
            log.info("决策树引擎=>{} userId：{} treeId：{} treeNode：{} ruleKey：{} matterValue：{}", treeRoot.getTreeName(), userId, treeId, treeNode.getTreeNodeId(), ruleKey, matterValue);
        }
        return treeNode;


    }

}